//
//  CrossPopView.swift
//  DSP
//
//  Created by tucici on 2024/5/27.
//

import UIKit
import TCCKit
class CrossPopView: UIView {
    private var tableView : UITableView!
    private var isshow : Bool = false
    var array = [FilterType]()
    var block:((_ type: FilterType)->Void)?
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func initUI(){
        bgHexColor = HEX_BG
        cornerRadius = 4.k_h
        borderColor = .black
        borderWidth = 0.5
        
        tableView = UITableView(frame:.zero, style:.plain)
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.tableHeaderView = UIView(frame:CGRect(x: 0, y: 0, width: 0, height: 0.01))
        tableView.tableFooterView = UIView(frame:CGRect(x: 0, y: 0, width: 0, height: 0.01))
        tableView.backgroundColor = .clear
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: UITableViewCell.ID())
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {}
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0
        } else {}
        addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        self.snp.makeConstraints { make in
            make.height.equalTo(0)
        }
    }
    
    func show(_ show:Bool = true){
        if isshow == show{return}
        isshow = show
        let currentFrame = self.frame
        let h = show ? Double(array.count) * 22 : 0.0
        let t = show ? 0.3 : 0.5
        UIView.animate(withDuration:t)  {[weak self] in
            self?.snp.updateConstraints { make in
                make.height.equalTo(h)
            }
            // UIView 的动画过程中,默认情况下视图是从中心点进行缩放和移动的。所以保持y轴的锚点固定，从上往下逐渐变化h
            self?.frame = CGRect(x: currentFrame.minX,
                                 y: currentFrame.minY,
                                 width: currentFrame.width,
                                 height: h)
            self?.layoutIfNeeded()
        }
    }
    
    func reset(){
        isshow = false
        let currentFrame = self.frame
        self.snp.updateConstraints { make in
            make.height.equalTo(0)
        }
        // UIView 的动画过程中,默认情况下视图是从中心点进行缩放和移动的。所以保持y轴的锚点固定，从上往下逐渐变化h
        self.frame = CGRect(x: currentFrame.minX,
                             y: currentFrame.minY,
                             width: currentFrame.width,
                             height: 0)
        self.layoutIfNeeded()
    }
}

//MARK: - UITableViewDataSource
extension CrossPopView:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: UITableViewCell.ID())!
        cell.contentView.removeAllSubView()
        cell.contentView.bgHexColor = HEX_BG
        cell.selectionStyle = .none
        
        let lbl = UILabel()
        lbl.text = array[indexPath.row].value
        lbl.textHexColor = HEX_BLACK
        lbl.font = font(14)
        lbl.minimumScaleFactor = 0.6
        lbl.adjustsFontSizeToFitWidth = true
        
        cell.contentView.addSubview(lbl)
        lbl.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.left.right.equalToSuperview().inset(5)
            make.height.equalTo(22)
        }
        return cell
    }
}
//MARK: - UITableViewDelegate
extension CrossPopView:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        show(false)
        block?(array[indexPath.row])
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}
